package org.tea.inventory.db.domain;

import cn.hutool.core.util.EnumUtil;
import org.tea.inventory.db.enums.IndustryTypeEnum;
import org.tea.inventory.db.util.CommonUtil;
import org.apache.commons.lang3.StringUtils;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

public class NewsCriterion {

    private String title;  // 标题
    private String key; // 关键字
    private String type; // 新闻类型 schoolInfo(校园动态)、alumniInfo(师生风采)、activity(校园活动)、activityNews(活动新闻)、lecture(讲座信息)
    // 、reading(悦读)、notice(公告管理)、video(公管视频)
    private String[] activityType; // 活动标签
    private int activityState; // 活动状态 1 - 报名进行中; 2 - 报名已截止; 3 - 报名待开始; 4 - 活动进行中; 5 - 活动已结束
    private String creator; // 创建者名称
    private int creatorId; // 创建者Id
    private int page = 1;
    private int limit = 10;
    private String sort;
    private LocalDate[] dateRange; // 时间范围：[活动开始时间, 活动结束时间]
    private int activityAuditState = -1; // 活动审核状态-0:待审核;1:审核通过;2:审核被拒
    private boolean myJoin = false; // true - 参加的活动
    private boolean commentOn = false; // true - 查询评论数量、点赞数量
    private int userIdForZan; // 用户查询点赞的id
    private int userId; //当前登录用户id

    private String sql;

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String[] getActivityType() {
        return activityType;
    }

    public void setActivityType(String[] activityType) {
        this.activityType = activityType;
    }

    public int getActivityState() {
        return activityState;
    }

    public void setActivityState(int activityState) {
        this.activityState = activityState;
    }

    public String getCreator() {
        return creator;
    }

    public void setCreator(String creator) {
        this.creator = creator;
    }

    public int getPage() {
        return page;
    }

    public void setPage(int page) {
        this.page = page;
    }

    public int getLimit() {
        return limit;
    }

    public void setLimit(int limit) {
        this.limit = limit;
    }

    public String getSort() {
        return sort;
    }

    public void setSort(String sort) {
        this.sort = sort;
    }

    public LocalDate[] getDateRange() {
        return dateRange;
    }

    public void setDateRange(LocalDate[] dateRange) {
        this.dateRange = dateRange;
    }

    public int getActivityAuditState() {
        return activityAuditState;
    }

    public void setActivityAuditState(int activityAuditState) {
        this.activityAuditState = activityAuditState;
    }

    public String getKey() {
        return key;
    }

    public void setKey(String key) {
        this.key = key;
    }

    public int getCreatorId() {
        return creatorId;
    }

    public void setCreatorId(int creatorId) {
        this.creatorId = creatorId;
    }

    public boolean isMyJoin() {
        return myJoin;
    }

    public void setMyJoin(boolean myJoin) {
        this.myJoin = myJoin;
    }

    public boolean isCommentOn() {
        return commentOn;
    }

    public void setCommentOn(boolean commentOn) {
        this.commentOn = commentOn;
    }

    public int getUserIdForZan() {
        return userIdForZan;
    }

    public void setUserIdForZan(int userIdForZan) {
        this.userIdForZan = userIdForZan;
    }

    public int getUserId() {
        return userId;
    }

    public void setUserId(int userId) {
        this.userId = userId;
    }

    public String getSql() {
        if(StringUtils.isBlank(sql)) {
            this.buildSql();
        }
        return sql;
    }

    public void setSql(String sql) {
        this.sql = sql;
    }

    private void buildSql() {
        this.sql = "1 = 1";

        if(StringUtils.isNotBlank(this.title)) {
            this.sql += " and news_title like '%" + this.title + "%'";
        }

        if(StringUtils.isNotBlank(this.key)) {
            this.sql += " and (news_title like '%" + this.key + "%' or news_content like '%" + this.key + "%')";
        }

        if(StringUtils.isNotBlank(this.creator)) {

        }

        if(this.creatorId != 0) {
            this.sql += " and creatorId = " + this.creatorId;
        }

        if(this.type != null) {
            System.out.println("this.type = " + this.type);
            if (Objects.equals(this.type, "dgtSiteNews")) {
                // 查询官网新闻3种类型的合集
                this.sql += " and news_type in ('companyNews','industryInfo','companyDynamic')";
            }else if (Objects.equals(this.type, "dgtIndustryNews") ){//todo 官网客户行业
                //查询官网行业类型所有
                List<Object> names = EnumUtil.getFieldValues(IndustryTypeEnum.class, "name");
                this.sql += " and news_type in ("+ CommonUtil.joinInSql(names) +")";
            } else {
                this.sql += " and news_type = '" + this.type + "'";
            }


            if(this.type.equals("activity")) {
                if(this.activityType != null) {
                    for(String str : this.activityType) {
                        this.sql += " and find_in_set('" + str + "', activity)";
                    }
                    //this.sql += " and activity = '" + this.activityType + "'";
                }

//                LocalDate nowDate = LocalDate.now();
//                if(this.activityState == 1) { // 报名进行中: 当前时间大于报名开始时间且小于报名截至时间
//                    this.sql += " and '" + nowDate + "' between date_format(activity_regist_start_date, '%Y-%m-%d')  and date_format(activity_regist_end_date, '%Y-%m-%d') ";
//                } else if(this.activityState == 2) { // 报名已截止: 当前时间大于报名截止时间，且小于活动开始时间
//                    this.sql += " and '" + nowDate + "' between date_format(activity_regist_end_date, '%Y-%m-%d') and date_sub(activity_start_date, interval 1 day)";
//                } else if(this.activityState == 3) { // 报名待开始: 当前时间小于报名开始时间
//                    this.sql += " and date_format(activity_regist_start_date, '%Y-%m-%d') > '" + nowDate + "'";
//                } else if(this.activityState == 4) { // 活动进行中: 当前时间大于活动开始时间，且小于活动结束时间
//                    this.sql += " and '" + nowDate + "' between date_format(activity_start_date, '%Y-%m-%d') and date_format(activity_end_date, '%Y-%m-%d')";
//                } else if(this.activityState == 5) { // 活动已结束: 当前时间大于活动结束时间
//                    this.sql += " and date_format(activity_end_date, '%Y-%m-%d') < '" + nowDate + "'";
//                }


                LocalDateTime nowDate = LocalDateTime.now();
                if(this.activityState == 1) { // 报名进行中: 当前时间大于报名开始时间且小于报名截至时间
                    this.sql += " and '" + nowDate + "' between activity_regist_start_date  and activity_regist_end_date ";
                } else if(this.activityState == 2) { // 报名已截止: 当前时间大于报名截止时间，且小于活动开始时间
                    this.sql += " and '" + nowDate + "' between activity_regist_end_date and activity_start_date ";
                } else if(this.activityState == 3) { // 报名待开始: 当前时间小于报名开始时间
                    this.sql += " and activity_regist_start_date > '" + nowDate + "'";
                } else if(this.activityState == 4) { // 活动进行中: 当前时间大于活动开始时间，且小于活动结束时间
                    this.sql += " and '" + nowDate + "' between activity_start_date and activity_end_date";
                } else if(this.activityState == 5) { // 活动已结束: 当前时间大于活动结束时间
                    this.sql += " and activity_end_date < '" + nowDate + "'";
                }

                if(this.dateRange != null) {
                    this.sql += " and date_format(activity_start_date, '%Y-%m-%d') >= '" + this.dateRange[0] + "' and date_format(activity_end_date, '%Y-%m-%d') <= '" + this.dateRange[1] + "'";
                }

                if(this.activityAuditState != -1) {
                    this.sql += " and activity_audit_state = " + this.activityAuditState;
                }

            } else if(this.type.equals("lecture")) {
                if(this.dateRange != null) {
                    this.sql += " and date_format(lecture_start_date, '%Y-%m-%d') between '" + this.dateRange[0] + "' and '" + this.dateRange[1] + "'";
                }
            } else {
                if(this.dateRange != null) {
                    this.sql += " and date_format(create_date, '%Y-%m-%d') between '" + this.dateRange[0] + "' and '" + this.dateRange[1] + "'";
                }
            }
        } else {
            this.sql += " and (case when alumni_news.news_type = 'activity' then alumni_news.activity_audit_state = 1 else alumni_news.activity_audit_state = 0 end) ";
        }


    }
}
